package com.objecteffects.ilmp.web.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.inject.Inject;

import com.objecteffects.ilmp.model.TumblrBlogEntity;
import com.objecteffects.ilmp.model.UserEntity;
import com.objecteffects.ilmp.service.UserEntityService;
import com.objecteffects.ilmp.util.logger.InjectLogger;
import com.objecteffects.ilmp.web.utils.LoginRequired;

import org.slf4j.Logger;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

/**
 * @author Rusty Wright
 */
@LoginRequired
public class AuthUrlsActionBean extends BaseActionBean {
    @InjectLogger
    private Logger log;

    private final UserEntityService userEntityService;

    private final List<String> tumblrUrls = new ArrayList<>();

    private List<String> selectedUrls = Collections.emptyList();

    @Inject
    AuthUrlsActionBean(final UserEntityService _userEntityService) {
        this.userEntityService = _userEntityService;
    }

    @DefaultHandler
    public Resolution startHandler() {
        final UserEntity userEntity =
            this.userEntityService.findById(this.getContext().getUserId());

        this.log.debug("userEntity: {}", userEntity);

        if (userEntity.getTumblrBlogEntities() != null) {
            for (final TumblrBlogEntity each : userEntity
                .getTumblrBlogEntities()) {
                this.tumblrUrls.addAll(each.getUrls());
            }
        }

        Collections.sort(this.tumblrUrls);

        return new ForwardResolution(IlmpView.AUTH_START);
    }

    public Resolution proceed() {
        final UserEntity userEntity =
            this.userEntityService.findById(this.getContext().getUserId());

        final List<TumblrBlogEntity> deletedBlogs = new ArrayList<>();

        for (final String url : this.selectedUrls) {
            this.log.debug("selected url: {}", url);

            /*
             * make sure the POST urls are urls that the user is
             * authorized for.
             */
            for (final TumblrBlogEntity blog : userEntity
                .getTumblrBlogEntities()) {
                this.log.debug("checking blog: {}", blog);

                if (blog.getUrls().contains(url)) {
                    this.log.debug("blog access token: {}",
                                   blog.getAccessToken());

                    deletedBlogs.add(blog);
                }
            }
        }

        userEntity.getTumblrBlogEntities().removeAll(deletedBlogs);

        userEntity.setTumblrName(null);

        this.userEntityService.update(userEntity);

        return new ForwardResolution(IlmpView.AUTH_DONE);
    }

    public List<String> getTumblrUrls() {
        return this.tumblrUrls;
    }

    public List<String> getSelectedUrls() {
        return this.selectedUrls;
    }

    public void setSelectedUrls(final List<String> _selectedUrls) {
        this.selectedUrls = _selectedUrls;
    }
}
